Skip to content

fix(examples): wire CORS_ORIGINS into the CORS middleware in 10-production-boilerplate - #40

Open
hashi-divyansh wants to merge 1 commit into
coregx:mainfrom
hashi-divyansh:fix/example-10-cors-origins
Open

hashi-divyansh wants to merge 1 commit into
coregx:mainfrom
hashi-divyansh:fix/example-10-cors-origins

Conversation

@hashi-divyansh

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #33 review. In examples/10-production-boilerplate, CORS_ORIGINS was loaded but never used: config.go wrapped the whole comma-separated value in a one-element []string, and main.go used middleware.CORS() (allow any origin).

  • Config.CORSOrigins is now a string. CORSConfig.AllowOrigins is already a comma-separated list, so no splitting is needed.
  • main.go uses middleware.CORSWithConfig(middleware.CORSConfig{AllowOrigins: cfg.CORSOrigins}).
  • README: documents the format and that * allows any origin.

Behavior change

Before After
Allowed origins * (any) CORS_ORIGINS (default http://localhost:3000,http://localhost:8080)
Allowed request headers * Content-Type,Accept,Authorization (CORSWithConfig default; covers this API)

Set CORS_ORIGINS=* to keep the old allow-all behavior.

Testing

  • go mod tidy && go build ./..., go vet ./..., gofmt, go test -race ./...
  • Manual preflight (OPTIONS with Authorization, Content-Type) and GET /health with an Origin header:
    • default: http://localhost:3000 allowed, http://evil.example gets no CORS headers
    • CORS_ORIGINS=https://app.example.com: that origin allowed, localhost:3000 gets no CORS headers
    • CORS_ORIGINS=*: any origin gets Access-Control-Allow-Origin: *

CORSOrigins wrapped the whole comma-separated env value in a
one-element []string and was never used: main.go called
middleware.CORS(), which allows any origin.

Make CORSOrigins a string (CORSConfig.AllowOrigins is already a
comma-separated list) and pass it to middleware.CORSWithConfig.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant